<template>
  <div>
    <el-breadcrumb separator="/" class="submenu">
      <el-breadcrumb-item>
        <span>工厂对应的物料信息</span>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <div class="main-right">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="物料编码:">
          <el-input v-model="formInline.materialCode" placeholder="物料编码"></el-input>
        </el-form-item>
        <el-form-item label="物料名称：">
          <el-input v-model="formInline.materialName" placeholder="物料名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(formInline)">查询</el-button>
          <el-button type="primary" @click="resetForm(formInline)">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- <el-button type="primary" @click="exemption('yes')" style="margin-bottom:20px">免检</el-button>
      <el-button type="primary" @click="exemption('no')" style="margin-bottom:20px">非免检</el-button>
      <el-button type="primary" style="margin-bottom:20px">导出</el-button> -->
      <el-table :data="tableData4" @selection-change="handleSelection" style="width: 100%" max-height="250" :default-sort="{prop: 'materialCode', order: 'descending'}">
        <el-table-column prop="id" v-if='isShow'></el-table-column>
        <!-- <el-table-column type="selection" width="55"></el-table-column> -->
        <el-table-column label="物料编码" sortable width="120">
          <template slot-scope="scope">
            <span @click="openVendorInfo(scope.row)" class="open-link">{{ scope.row.materialCode }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="materialName" label="物料名称" width="250"></el-table-column>
        <el-table-column prop="specification" label="规格型号" width="150"></el-table-column>
        <el-table-column prop="description" label="描述" width="200"></el-table-column>
        <el-table-column prop="cpFlag" :formatter="cpFlagFormat" label="是否有集采权限" width="150"></el-table-column>
        <el-table-column prop="cpActivateFlag"  :formatter="cpFlagFormat" label="是否启用集采" width="150"></el-table-column>
        <el-table-column prop="freezeFlag" :formatter="cpFlagFormat" label="是否冻结" width="150"></el-table-column>
        <el-table-column prop="exemptionFlag" :formatter="cpFlagFormat" label="是否免检" width="150"></el-table-column>
        <el-table-column prop="remarks" label="备注" width="250"></el-table-column>
      </el-table>
      <page></page>
    </div>
    <div>
      <el-dialog :model="form2" title="供应商信息" :visible.sync="dialogFormVisible">
        <el-row>
          <el-col :span="3"><el-input value="物料编码：" :disabled="true"></el-input></el-col>
          <el-col :span="9"><el-input v-model="form2.materialCode" :disabled="true"></el-input></el-col>
          <el-col :span="3"><el-input value="物料名称：" :disabled="true"></el-input></el-col>
          <el-col :span="9"><el-input v-model="form2.materialName" :disabled="true"></el-input></el-col>
        </el-row>
        <!-- <el-button type="primary" @click="exemption('yes')" style="margin-bottom:20px">货源配置</el-button> -->
        <el-table :data="tableData5" @selection-change="handleSelection" style="width: 100%" max-height="250" :default-sort="{prop: 'materialCode', order: 'descending'}">
          <el-table-column prop="id" v-if='isShow'></el-table-column>
          <el-table-column prop="materialCode" v-if='isShow'></el-table-column>
          <el-table-column prop="materialId" v-if='isShow'></el-table-column>
          <!-- <el-table-column prop="operation" align ="center" width="300" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="setStatus(scope.row, 'exemptionFlag', 1)" size="small">免检</el-button>
              <el-button type="text" @click="setStatus(scope.row, 'exemptionFlag', 0)" size="small">非免检</el-button>
              <el-button type="text" @click="setStatus(scope.row, 'stockFeedbackStatus', 1)" size="small">需要反馈</el-button>
              <el-button type="text" @click="setStatus(scope.row, 'stockFeedbackStatus', 0)" size="small">无需反馈</el-button>
            </template>
          </el-table-column> -->
          <el-table-column prop="vendorCode" label="供应商编码" width="150"></el-table-column>
          <el-table-column prop="vendorErpCode" label="供应商ERP编号" width="250"></el-table-column>
          <el-table-column prop="vendorName" label="供应商名称" width="250"></el-table-column>
          <el-table-column prop="exemptionFlag" :formatter="exemptionFlagFormat" label="是否免检" width="150"></el-table-column>
          <el-table-column prop="stockFeedbackStatus" :formatter="stockFeedbackFormat" label="是否反馈库存" width="150"></el-table-column>
        </el-table>
        <page></page>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
  import axios from 'axios'
  import Page from '@/components/page.vue'
  export default {
    components: {
      Page
    },
    data() {
      return {
        activeName: 'first',
        dialogFormVisible: false,
        formInline: {
          materialCode: '',
          materialName: ''
        },
        tableData4: [],
        tableData5: [],
        multipleSelection: [],
        checkIds:[],
        formLabelWidth: '120px',
        form: {
        },
        form2: {
        }
      };
    },
    mounted() {
      this.tableData4f();
    },
    created(){
      // console.log(this.$route.params.id)
      console.log(this.$route.query.id)
    },
    methods: {
       cpFlagFormat(row, column){
        if(row.cpFlag == 1){
          return '是';
        } else {
            return '否';
        }
       },
       exemptionFlagFormat(row, column){
        if(row.exemptionFlag == 1){
          return '是';
        } else {
          return '否';
        }
       },
       stockFeedbackFormat(row, column){
        if(row.stockFeedbackStatus == 1){
          return '是';
        } else {
          return '否';
        }
       },
       tableData4f() {
        axios.post("http://localhost:8763/mdata/material/getMaterirlByFacId/" + this.$route.query.factoryId, {})
          .then((response) => {
            console.log(response);
            this.tableData4 = response.data.rows;
          })
          .catch((error) => {
            console.log(error);
          });
      },
      //查询
      submitForm(formInline) {
        console.log(formInline);
        axios.post("http://localhost:8763/mdata/material/getMaterirlByFacId/" + this.$route.query.factoryId, formInline)
          .then(response => {
            console.log(response);
            this.tableData4 = response.data.rows;
          }).catch(error => {
            console.log('网络错误，不能访问');
          });
      },
      exemption(exemptionFlag) {
        // 免检、非免检操作
        let checkRows = this.multipleSelection;
        checkRows.map(item =>{
          console.log(item.id);
          this.checkIds.push(item.id);
        });
        axios.post("http://localhost:8763/mdata/material/changeExemption/" + exemptionFlag + "/" + this.$route.query.factoryId, this.checkIds)
        .then((response)=>{
            this.tableData4f()
        })
        .catch((error)=>{
          console.log(error);
        });
      },
      openVendorInfo(row){
        // 打开供应商信息弹框
        this.dialogFormVisible = true,
        this.form2 = row
        this.tableData5f(row.id);
      },
      tableData5f(materialId) {
        axios.post("http://localhost:8763/mdata/materialVendor/getVendorByMatId/" + materialId + '/' +  this.$route.query.factoryId, {})
          .then((response) => {
            console.log(response);
            this.tableData5 = response.data.rows;
          })
          .catch((error) => {
            console.log(error);
          });
      },
      // 供应商物料免检、非免检、是否反馈操作
      setStatus(row, status, value) {
        axios.post("http://localhost:8763/mdata/materialVendor/updateStatus2/" + row.id + "/" + status + "/" + value)
        .then((response)=>{
            this.tableData5f(row.materialId)
        })
        .catch((error)=>{
          console.log(error);
        });
      },
      // 赋值多选选择
      handleSelection(val) {
        this.multipleSelection = val;
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style>

</style>
